package com.ohs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ohs.mapper.before.AddressMapper;
import com.ohs.mapper.before.DistrictMapper;
import com.ohs.mapper.before.OrderMapper;
import com.ohs.pojo.Address;
import com.ohs.pojo.District;
import com.ohs.pojo.Order;
import com.ohs.pojo.Order_Product;
import com.ohs.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class OrderSeriveImpl implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Autowired(required = false)
    private DistrictMapper districtMapper;
    @Override
    public int selectCountUnpaid(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("status","待支付");
        return orderMapper.selectCount(queryWrapper);
    }

    @Override
    public List<Order_Product> selectListUnpaid(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        List<Order_Product> list = orderMapper.selectListByStatus("待支付",userId);
        for(int i=0;i<list.size();i++){
            Order_Product order_product = list.get(i);
            Double price = order_product.getPrice();
            Integer num =order_product.getNum();
            Double total=price*num;
            order_product.setTotal(total);
            list.set(i,order_product);
        }
        return list;
    }

    @Override
    public int selectCountDaiShou(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("status","待收货");
        return orderMapper.selectCount(queryWrapper);
    }

    @Override
    public List<Order_Product> selectListDaishou(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        List<Order_Product> list = orderMapper.selectListByStatus("待收货",userId);
        for(int i=0;i<list.size();i++){
            Order_Product order_product = list.get(i);
            Double price = order_product.getPrice();
            Integer num =order_product.getNum();
            Double total=price*num;
            System.out.println(total);
            order_product.setTotal(total);
            list.set(i,order_product);
        }
        return list;
    }

    @Override
    public int selectCountDaiYiShou(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","已收货");
        queryWrapper.eq("userId",userId);
        return orderMapper.selectCount(queryWrapper);
    }

    @Override
    public List<Order_Product> selectListYiShou(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        List<Order_Product> list = orderMapper.selectListByStatus("已收货",userId);
        for(int i=0;i<list.size();i++){
            Order_Product order_product = list.get(i);
            Double price = order_product.getPrice();
            Integer num =order_product.getNum();
            Double total=price*num;
            order_product.setTotal(total);
            list.set(i,order_product);
        }
        return list;
    }

    @Override
    public List<Order_Product> selectListAll(HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        List<Order_Product> list = orderMapper.selectListAll(userId);
        for(int i=0;i<list.size();i++){
            Order_Product order_product = list.get(i);
            Double price = order_product.getPrice();
            Integer num =order_product.getNum();
            Double total=price*num;
            order_product.setTotal(total);
            list.set(i,order_product);
        }
        return list;
    }
    @Override
    public Order_Product findOrderByOrderId(Integer orderId,HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        Order_Product order_product= this.orderMapper.findOrderOne(orderId,userId);
        return order_product;
    }

    @Override
    public Address findAddressOne(Integer addId,HttpSession session) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addId",addId);
        Address address= this.addressMapper.selectOne(queryWrapper);
        String province= address.getProvince();
        String city=address.getCity();
        String area=address.getArea();
        District district= this.districtMapper.findDistrictByCode(province);
        String provinceName= district.getName();
        District district1= this.districtMapper.findDistrictByCode(city);
        String cityName= district1.getName();
        District district2= this.districtMapper.findDistrictByCode(area);
        String areaName= district2.getName();
        String detail=address.getDetail();
        String detailName=provinceName+cityName+areaName+detail;
        address.setDetail(detailName);
        return address;
    }

    @Override
    public List<Order_Product> findOrder_Product(String proName,HttpSession session) {
        Integer userId=(Integer) session.getAttribute("userId");
        List<Order_Product> list= this.orderMapper.findOrder_Product(proName,userId);
        for(int i=0;i<list.size();i++){
            Order_Product order_product = list.get(i);
            Double price = order_product.getPrice();
            Integer num =order_product.getNum();
            Double total=price*num;
            order_product.setTotal(total);
            list.set(i,order_product);
        }
        return list;
    }

    @Override
    public String  updateOrderByOrderId(Integer orderId) {
        int row= this.orderMapper.updateOrderByorderId(orderId);
        if(row!=1){
            return "redirect:toMyorderDaiShouhuo";
        }
        return "redirect:toMyorderYiShou";
    }
}
